package com.qujia.station.controller;

import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.Grade;
import com.qujia.station.domain.ShiftClass;
import com.qujia.station.domain.Station;
import com.qujia.station.service.IGradeService;
import com.qujia.station.service.IOilService;
import com.qujia.station.service.IShiftMaskService;
import com.qujia.station.service.IStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 油站信息Controller
 *
 * @author qujia
 * @date 2020-12-08
 */
@RestController
@RequestMapping("/shiftClass/info")
public class ShiftClassController extends BaseController
{
    @Autowired
    private IShiftMaskService shiftMaskService;
    @Autowired
    private IStationService stationService;

    /**
     * 查询等级信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ShiftClass shiftClass)
    {

        LoginUser current=SecurityUtils.getLoginUser();
        if("1".equals(current.getUser().getUserLevel())){
            shiftClass.setEnterpriseId(current.getUser().getEnterpriseId());
        }else if("2".equals(current.getUser().getUserLevel())) {
            if(current.getUser().getStationId()!=null){
                Station station=stationService.selectStationById(current.getUser().getStationId());
                shiftClass.setEnterpriseId(station.getEnterpriseId());
            }
        }else {
            return getDataTable(Collections.emptyList());
        }
        startPage();
        List<ShiftClass> shiftClassList = shiftMaskService.selectShiftClassList(shiftClass);
        shiftClassList.get(0).getCreateTime().getTime();
        return getDataTable(shiftClassList);
    }

    /**
     * 新增油品
     */
    @Log(title = "新增班次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShiftClass shiftClass)
    {
        LoginUser current=SecurityUtils.getLoginUser();
        if(!"1".equals(current.getUser().getUserLevel())){
            return  AjaxResult.error("油企才能配置班次信息");
        }

        shiftClass.setEnterpriseId(current.getUser().getEnterpriseId());
        shiftClass.setCreateTime(new Date());
        shiftClass.setCreateUser(current.getUser().getUserId());
        return toAjax(shiftMaskService.insertShiftClass(shiftClass));
    }




    /**
     * 获取等级信息详细信息
     */
    @GetMapping(value = "/{shiftClassId}")
    public AjaxResult getInfo(@PathVariable("shiftClassId") Long shiftClassId)
    {
        ShiftClass shiftClass=shiftMaskService.selectShiftClassById(shiftClassId);

        return AjaxResult.success(shiftClass);
    }

    /**
     * 修改油站信息
     */
    @Log(title = "修改班次信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShiftClass shiftClass)
    {
        LoginUser current=SecurityUtils.getLoginUser();
        if(!"1".equals(current.getUser().getUserLevel())){
            return  AjaxResult.error("油企才能配置班次信息");
        }
        shiftClass.setEnterpriseId(current.getUser().getEnterpriseId());
        shiftClass.setUpdateTime(new Date());
        shiftClass.setUpdateUser(current.getUser().getUserId());
        return toAjax(shiftMaskService.updateShiftClass(shiftClass));
    }



}
